//
//  HNBankRecordController.m
//  XDEducation
//
//  Created by 星道 on 17/5/22.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HNBankRecordController.h"
#import "XDMiyoshiBankTodayViewController.h"

@interface HNBankRecordController ()
@property (nonatomic, strong) NSMutableArray *DataArr;
@property (nonatomic, assign) NSInteger Index;
@end

@implementation HNBankRecordController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.Index = 1;
    
    [MBProgressHUD showHUD];
    
    [self RefreshData];
    
    [self ConfigTableView];
}




#pragma mark - Config
- (void)ConfigTableView
{
    
    self.tableView.tableFooterView = [UIView new];
    self.tableView.tableHeaderView = [UIView new];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self RefreshData];
    }];
}




#pragma mark - 网络请求
- (void)RefreshData
{
    NSDictionary *Dict = @{
                           @"uid"   :[AccountTool shareAccountTool].currentAccount.uid,
                           @"page"  :@(self.Index),
                           @"role"  :@4
                           };
    [XDHttpTool get:BaseURL(@"user.do?action=getMyFruitHistoryTime") authCode:[AccountTool shareAccountTool].currentAccount.authCode params:Dict success:^(id responseObj) {
        [self EndRefresh];
        if (responseObj)
        {
            if ([responseObj count] == 0 && [self.DataArr count] > 0)
            {
                dispatch_async(dispatch_get_main_queue(), ^{
                   [self.tableView.mj_footer endRefreshingWithNoMoreData];
                });
            }
            else
            {
                for (NSString *Str in responseObj)
                {
                    [self.DataArr addObject:Str];
                }
                self.Index += 1;
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableView reloadData];
                });
            }
        }
        else
        {
            [MBProgressHUD showError:responseObj[@"data"][@"msg"]];
        }
    } failure:^(NSError *error) {
        [self EndRefresh];
        [MBProgressHUD showError:XDNetWorkError];
    }];
}

//结束刷新
- (void)EndRefresh
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    });
}



#pragma mark - TableView代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.DataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!Cell)
    {
        Cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"Cell"];
        Cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        Cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    Cell.textLabel.text = self.DataArr[indexPath.row];
    return Cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    XDMiyoshiBankTodayViewController *VC = [[XDMiyoshiBankTodayViewController alloc] init];
    VC.DateTime = self.DataArr[indexPath.row];
    VC.XDMiyoshiBankTodayType = XDMiyoshiBankTodayJiLu;
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark - Setter
- (NSMutableArray *)DataArr
{
    if (!_DataArr)
    {
        _DataArr = [NSMutableArray array];
    }
    return _DataArr;
}
@end
